package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 用户下单
     * @param orders
     */
    @Override
    @Transactional
    public void submit(Orders orders) {
        Long userId = BaseContext.getCurrentId();  //用户id
        User user = userService.getById(userId); //用户对象
        Long addressBookId = orders.getAddressBookId(); //地址id // 前端页面传过来的.直接获取
        AddressBook addressBook = addressBookService.getById(addressBookId);//地址对象
        //订单总金额
        BigDecimal total = new BigDecimal(0);

        //获取购物车里的数据
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);

        //自己定义id
        long ordersId = IdWorker.getId();
        //2.添加订单详情表的数据
        //2.1遍历购物车数据的集合
        List<OrderDetail> orderDetailsList = new ArrayList<>();
        for (ShoppingCart cart : shoppingCartList) {
            //将购物车里的数据赋值给订单详情表
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());
            orderDetail.setOrderId(ordersId);//orders里面已经存入了id
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            //小计金额
            orderDetail.setAmount(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
            //金额总计
            total = total.add(orderDetail.getAmount());

            orderDetail.setImage(cart.getImage());
            orderDetailsList.add(orderDetail);
        }

        //1.添加订单表的数据

        orders.setId(ordersId);
        orders.setNumber("o_"+System.currentTimeMillis());
        orders.setStatus(2);
        orders.setUserId(user.getId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(total);
        orders.setUserName(user.getName());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        //调用orders本类方法直接保存
        this.save(orders);

        orderDetailService.saveBatch(orderDetailsList);
    }
}
